package com.app.controller;

import com.app.conf.DataBaseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.Map;

@RestController
@Controller
@Slf4j
@RequestMapping("/db")
public class DataBaseController {
//    　 /* 备份数据库 */
    @GetMapping("backupSQL")
    public ModelAndView backupSQL(Map<String, Object> map){

        String fileName = "backup_" + new Date().getTime() + ".sql";
        String cmd =  "cmd/C mysqldump -uroot -p123456 office_manage > " + "e:/backUp/" + fileName; //-u后的root为mysql数据库用户名，-p后接的123456为该用户密码，注意不要有空格；dbName填写需要备份数据的数据库名称，大于号后接生成文件路径
        try {
            DataBaseUtils.exportDatabaseTool("localhost","root","dsz0704","e:/backUp/","backup_","office_manage");
            Runtime.getRuntime().exec(cmd);
        }catch (Exception e){
            log.error("【备份数据库】失败：{}", e.getMessage());
            map.put("msg", e.getMessage());
            return new ModelAndView("common/error", map);
        }
        log.info("【备份数据库】成功，SQL文件：{}", fileName);
        map.put("msg","备份数据库成功");return new ModelAndView("common/success", map);
    }
}
